<?php

namespace app\admins\controller;

use think\Controller;
use Util\SysDb;

class Release extends Controller
{
    public function del()
    {
        $id = input('param.releaseId');
        $releaseAuthor = input('param.releaseAuthor');
        $this->db = new SysDb();
        $del = $this->db->table('releaselist')->where(['releaseAuthor' => $releaseAuthor])->where(['releaseId' => $id])->delete();
        if ($del) {
            $resetId = $this->db->table('releaselist')->where(['releaseAuthor' => $releaseAuthor])->lists();
            $re = 1;
            foreach ($resetId as $rid) {
                if ($re < 10) {
                    $re = '0'.$re;
                }
                $this->db->table('releaselist')->where(['releaseAuthor' => $releaseAuthor])->where(['id' => $rid['id']])->update(['releaseId' => $re.'_'.$releaseAuthor, 'id' => $re.'_'.$releaseAuthor]);
                ++$re;
            }
            $re = 1;
            exit(json_encode(['code' => 0, 'msg' => '删除成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '删除失败']));
        }
    }

    public function allDel()
    {
        $allId = input('param.allId');
        $releaseAuthor = input('param.releaseAuthor');

        $this->db = new SysDb();
        foreach ($allId as $id) {
            $del = $this->db->table('releaselist')->where(['releaseAuthor' => $releaseAuthor])->where(['releaseId' => $id])->delete();
        }
        if ($del) {
            $resetId = $this->db->table('releaselist')->where(['releaseAuthor' => $releaseAuthor])->lists();
            $re = 1;
            foreach ($resetId as $rid) {
                if ($re < 10) {
                    $re = '0'.$re;
                }
                $this->db->table('releaselist')->where(['releaseAuthor' => $releaseAuthor])->where(['id' => $rid['id']])->update(['releaseId' => $re.'_'.$releaseAuthor, 'id' => $re.'_'.$releaseAuthor]);
                ++$re;
            }
            $re = 1;
            exit(json_encode(['code' => 0, 'msg' => '删除成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '删除失败']));
        }
    }

    public function ReleaseAdd()
    {
        $releaseName = input('param.releaseName');
        $abstract = input('param.abstract');
        $content = input('param.content');
        $classify = input('param.classify');
        $teacher = input('param.teacher');
        $releaseStatus = input('param.releaseStatus');
        $releaseTime = input('param.releaseTime');
        $endTime = input('param.endTime');
        $releaseTop = input('param.releaseTop');
        $releaseAuthor = input('param.releaseAuthor');
        $CourseCode = input('param.CourseCode');
       
        $this->db = new SysDb();
        $count = $this->db->table('releaselist')->where(['Did' => 't1_1'])->where(['releaseAuthor' => $releaseAuthor])->count();
        $data = [];
        $data['Did'] = 't1_1';
        if ($count <9) {
            $count = $count + 1;
            $data['id'] = $data['releaseId'] = '0'.$count.'_'.$releaseAuthor;
        } else {
            $count = $count + 1;
            $data['id'] = $data['releaseId'] = $count.'_'.$releaseAuthor;
        }

        $data['releaseName'] = $releaseName;
        $data['releaseAuthor'] = $releaseAuthor; //传入session中的用户名
        $data['abstract'] = $abstract;
        $data['classify'] = $classify;
        $data['Teacher'] = $teacher;
        // $data['releaseImg'] = $releaseImg;
        $data['releaseStatus'] = $releaseStatus;
        $data['releaseLook'] = '开放浏览'; //是否公开浏览
        $data['releaseTop'] = $releaseTop;
        $data['releaseTime'] = $releaseTime;
        $data['endTime'] = $endTime;
        $data['content'] = $content;
        $data['CourseCode'] = $CourseCode;
        
        $add = $this->db->table('releaselist')->insert($data);
        if ($add) {
            exit(json_encode(['code' => 0, 'msg' => '添加成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '添加失败']));
        }
    }

    public function ReleaseUpdate()
    {
        $id = input('param.id');
        $releaseName = input('param.releaseName');
        $abstract = input('param.abstract');
        $content = input('param.content');
        $classify = input('param.classify');
        $releaseStatus = input('param.releaseStatus');
        $releaseTime = input('param.releaseTime');
        $endTime = input('param.endTime');
        //$releaseTop = input('param.releaseTop');
        $releaseAuthor = input('param.releaseAuthor');

        $this->db = new SysDb();

        $data = [];
        $data['Did'] = 't1_1';
        $data['id'] = $id;
        $data['releaseName'] = $releaseName;
        $data['releaseAuthor'] = $releaseAuthor; //传入session中的用户名
        $data['abstract'] = $abstract;
        $data['classify'] = $classify;
        // $data['releaseImg'] = $releaseImg;
        $data['releaseStatus'] = $releaseStatus;
        $data['releaseLook'] = '开放浏览'; //是否公开浏览
        //$data['releaseTop'] = $releaseTop;
        $data['releaseTime'] = $releaseTime;
        $data['endTime'] = $endTime;
        $data['content'] = $content;
        $update = $this->db->table('releaselist')->where(['releaseAuthor' => $releaseAuthor])->where(['id' => $id])->update($data);
        if ($update) {
            exit(json_encode(['code' => 0, 'msg' => '编辑成功']));
        } else {
            exit(json_encode(['code' => 1, 'msg' => '编辑失败']));
        }
    }
}
